//
//  FHYHomePageViewController.m
//  Education
//
//  Created by jxyeagle on 14-8-14.
//  Copyright (c) 2014年 姬鑫宇. All rights reserved.
//

#import "FHYHomePageViewController.h"
#import "FHYLoginViewController.h"
#import "ConnectControl.h"

@interface FHYHomePageViewController ()<ConnectControlDelegate>
{
    ConnectControl *connectCtr;
    UIView *backGroundView;
}
@end

@implementation FHYHomePageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    backGroundView = [[UIView alloc]initWithFrame:self.view.frame];
    backGroundView.backgroundColor = [UIColor blackColor];
    backGroundView.alpha = 0.5;
    
    NSUserDefaults *userDefault =[NSUserDefaults standardUserDefaults];
    NSString *userName = [userDefault objectForKey:@"userName"];
    
    if (!userName) {
        [self goToLogin];
    }else{
        [self.view addSubview:backGroundView];
        
        connectCtr = [ConnectControl showForView:self.view];
        connectCtr.delegate=self;
        
        if (connectCtr.superview!=nil && connectCtr.loading==NO)
        {
            [connectCtr setLoading:YES];
        }
    }
    
    // Do any additional setup after loading the view.
}

- (void)connect{
    NSUserDefaults *userDefault =[NSUserDefaults standardUserDefaults];
    NSString *userName = [userDefault objectForKey:@"userName"];
    NSString *userCode = [userDefault objectForKey:@"userCode"];
    
    [FHYModel loginWithBlock:^(BOOL result, FHYLogInModel *userDic, NSString *failedMessage, NSError *error) {
        [ConnectControl removeFromView:self.view];
        [backGroundView removeFromSuperview];
        
        if (result) {
            return;
        }else{
            [self goToLogin];
        }
    } withUserName:userName password:userCode];
}

- (void)goToLogin{
    UIStoryboard * MainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    FHYLoginViewController *vc  = [MainStoryBoard instantiateViewControllerWithIdentifier:@"FHYLoginViewController"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)newsBtnPress:(id)sender {
}

- (IBAction)testBtnPress:(id)sender {
}

- (IBAction)moreBtnPress:(id)sender {
}

- (IBAction)lessonBtnPress:(id)sender {
}

- (IBAction)classBtnPress:(id)sender {
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
